package com.spic.system.service.impl;

import com.spic.common.core.utils.bean.BeanUtils;
import com.spic.common.security.utils.cache.DeptCacheUtils;
import com.spic.system.api.domain.SysUser;
import com.spic.system.config.Group;
import com.spic.system.config.ValidatorUtil;
import com.spic.system.domain.UserAll;
import com.spic.system.domain.UserAllDirty;
import com.spic.system.mapper.SysUserMapper;
import com.spic.system.mapper.UserAllDirtyMapper;
import com.spic.system.mapper.UserAllMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author : 赵祥
 * create at:  2022/11/14  15:20
 * @Description :
 **/
@Service
public class UserAllService {
    @Autowired
    private UserAllMapper userAllMapper;
    @Autowired
    private ThreadPoolExecutor executor;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private UserAllDirtyMapper userAllDirtyMapper;

    public List<UserAll> selectUserAllList(UserAll userAll) {
        List<UserAll> list = userAllMapper.selectUserAllList(userAll);
        for (UserAll user : list) {
            try {
                user.setParentDeptName(DeptCacheUtils.getValue(user.getParentDeptId()));
                user.setDeptName(DeptCacheUtils.getValue(user.getDeptId()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public void syncUser2All() {
        SysUser user1 = new SysUser();
        user1.setDelFlag("0");
        List<SysUser> sysUsers = sysUserMapper.selectUserList(user1);
        executor.execute(() -> {
            for (SysUser user : sysUsers) {
                UserAll userAll = new UserAll();
                userAll.setPersonnelCode(user.getUserName());
                userAll.setDeptId(user.getDeptId());
                userAll.setName(user.getNickName());
                if(user.getDept() != null) {
                    userAll.setParentDeptId(user.getDept().getParentId());
                }
                userAll.setEmail(user.getEmail());
                userAll.setPhoneNumber(user.getPhonenumber());
                //用户性别（0男 1女 2未知）
                String sex = user.getSex();
                sex = "0".equals(sex) ? "男" : "1".equals(sex) ? "女" : "未知";
                userAll.setSex(sex);
                userAll.setSource(0);
                UserAll userAllParam = new UserAll();
                userAllParam.setPersonnelCode(user.getUserName());
                List<UserAll> list = userAllMapper.selectUserAllList(userAllParam);
                if (list.size() == 0) {
                    userAllMapper.insertUserAll(userAll);
                } else {
                    userAllMapper.updateUserAll(userAll);
                }
            }
        });
    }

    public void filterDirtyData() {
        userAllDirtyMapper.deleteAll();
        List<UserAll> list = userAllMapper.selectUserAllList(new UserAll());
        for (UserAll userAll : list) {
            Integer source = userAll.getSource();
            //安全系统的数据
            if(source == 0) {
                List<String> validate = ValidatorUtil.validate(userAll, Group.SystemGroup.class);
                if(validate != null && validate.size() > 0) {
                    String dirtyReason = String.join(",", validate);
                    UserAllDirty dirty = new UserAllDirty();
                    dirty.setIncomplete(1);
                    dirty.setDirtyReason(dirtyReason);
                    BeanUtils.copyBeanProp(dirty, userAll);
                    userAllDirtyMapper.insertUserAllDirty(dirty);
                }
            }
            //HR系统导入的数据
            if(source == 1) {
                List<String> validate = ValidatorUtil.validate(userAll, Group.HRGroup.class);
                if(validate != null && validate.size() > 0) {
                    String dirtyReason = String.join(",", validate);
                    UserAllDirty dirty = new UserAllDirty();
                    dirty.setIncomplete(1);
                    dirty.setDirtyReason(dirtyReason);
                    if(dirtyReason.contains("年龄有误")) {
                        dirty.setIncorrect(1);
                    }
                    BeanUtils.copyBeanProp(dirty, userAll);
                    userAllDirtyMapper.insertUserAllDirty(dirty);
                }
            }
        }

    }

}


